package com.syq.face.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.syq.face.R;
import com.syq.face.model.ManagementGroupInfo;

import java.util.List;

public class GroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ManagementGroupInfo> managementGroupInfoList;
    private MyViewHolder.OnItemClickListener onItemClickListener;

    public MyViewHolder.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(MyViewHolder.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setManagementGroupInfoList(List<ManagementGroupInfo> managementGroupInfoList) {
        this.managementGroupInfoList = managementGroupInfoList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.group_list_item, parent, false));
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            ManagementGroupInfo groups = managementGroupInfoList.get(position);
            ((MyViewHolder) holder).titileTextView.setText(groups.getName());
            ((MyViewHolder) holder).contentTextView.setText(groups.getDescription());
            ((MyViewHolder) holder).countTextView.setText(groups.getCount().toString());
            ((MyViewHolder) holder).adminNameText.setText(groups.getAdminName());
            ((MyViewHolder) holder).codeText.setText(groups.getCode());

//            if(xxx){
//                ((MyViewHolder) holder).adminNameText.setVisibility(View.GONE);
//            }

            ((MyViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position, groups.getId());
                  //      attendanceFragment.notifyDataReload();
                    }
                }
            });


        }
    }

    @Override
    public int getItemCount() {
        int size = managementGroupInfoList == null ? 0 : managementGroupInfoList.size();
        return size;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView titileTextView;
        private TextView contentTextView;
        private TextView countTextView;
        private TextView adminNameText;
        private TextView codeText;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            titileTextView = itemView.findViewById(R.id.index_gl_item_title);
            contentTextView = itemView.findViewById(R.id.index_gl_item_titlecontent);
            countTextView = itemView.findViewById(R.id.index_gl_item_count);
            adminNameText = itemView.findViewById(R.id.index_gl_item_admin_name);
            codeText=itemView.findViewById(R.id.index_gl_item_code);

        }

        public interface OnItemClickListener {
            void onItemClick(int position, String groupId);
        }

    }


}
